package com.shenglaotuan.community.tag.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.shenglaotuan.community.article.entity.Article;
import com.shenglaotuan.community.article.service.ArticleService;
import com.shenglaotuan.community.article_tag.entity.ArticleTag;
import com.shenglaotuan.community.article_tag.service.ArticleTagService;
import com.shenglaotuan.community.tag.entity.Tag;
import com.shenglaotuan.community.tag.mapper.TagMapper;
import com.shenglaotuan.community.tag.service.TagService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.shenglaotuan.community.user_tag.entity.UserTag;
import com.shenglaotuan.community.user_tag.service.UserTagService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collector;
import java.util.stream.Collectors;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author ruidong
 * @since 2020-08-22
 */
@Service
public class TagServiceImpl extends ServiceImpl<TagMapper, Tag> implements TagService {

    @Autowired
    private UserTagService userTagService;

    @Override
    @Transactional
    public List<String> addTag(String uid, String[] tags) {
        tags = Arrays.stream(tags).filter(tag -> StringUtils.isNotBlank(tag)).toArray(String[] :: new);
        // 查询标签名是否已存在
        List<Tag> existTags = list(new QueryWrapper<Tag>().in("name", tags).select("id", "name"));
        // 将标签名取出存入列表
        List<String> existTagNames = existTags.stream().map(tag -> tag.getName()).collect(Collectors.toList());
        // 获取标签名id，之后存入文章标签表
        List<String> tagIds = existTags.stream().map(tag -> tag.getId()).collect(Collectors.toList());
        // 用于存放需要存入标签表的数据
        List<Tag> saveTagList = new ArrayList<>();
        // 将不存在的标签名存入
        for (String tag : tags) {
            // 将不存在的标签存入
            if(!existTagNames.contains(tag)){
                saveTagList.add(new Tag(tag));
            }
        }
        // 存入标签
        saveBatch(saveTagList);
        // 将标签id整合
        tagIds.addAll(saveTagList.stream().map(tag -> tag.getId()).collect(Collectors.toList()));
        // 查询用户标签表中该用户是否有这些标签，若没有，则添加
        List<String> existUserTags = userTagService.list(
                new QueryWrapper<UserTag>()
                        .select("tid")
                        .eq("uid", uid)
                        .in("tid", tagIds)
        ).stream().map(userTag -> userTag.getTid()).collect(Collectors.toList());
        // 将不存在的标签过滤出
        List<UserTag> noExistUserTags = tagIds.stream()
                .filter(tid -> !existUserTags.contains(tid))
                .map(tid -> new UserTag(uid, tid))
                .collect(Collectors.toList());
        // 将用户标签存入数据库
        userTagService.saveBatch(noExistUserTags);

        // 将该用户已存在的标签名数组返回
        return existTags.stream()
                .filter(tag -> existUserTags.contains(tag.getId()))
                .map(tag -> tag.getName())
                .collect(Collectors.toList());
    }

    @Override
    public List<Tag> listUserTag(String uid) {
        // 获取用户所拥有的标签
        List<String> tids = userTagService.list(new QueryWrapper<UserTag>()
                .select("tid")
                .eq("uid", uid))
                .stream().map(userTag -> userTag.getTid()).collect(Collectors.toList());
        if(tids.isEmpty()){
            return null;
        }
        // 获取获取标签名
        return list(new QueryWrapper<Tag>().select("id","name").in("id", tids));
    }
}
